package com.sp.biz.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sp.biz.user.bo.UserInfoBO;
import com.sp.biz.user.entity.UserInfoDO;
import com.sp.biz.user.entity.UserPostRelationDO;
import com.sp.biz.user.entity.UserRoleRelationDO;
import com.sp.biz.user.mapper.UserInfoMapper;
import com.sp.biz.user.query.UserInfoParam;
import com.sp.biz.user.service.UserInfoService;
import com.sp.biz.user.service.UserPostRelationService;
import com.sp.biz.user.service.UserRoleRelationService;
import com.sp.biz.user.vo.UserInfoVO;
import com.sp.common.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2025-07-16 18:47:51
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfoDO> implements UserInfoService {

    @Autowired
    private UserRoleRelationService userRoleRelationService;

    @Autowired
    private UserPostRelationService userPostRelationService;


    @Override
    public Page<UserInfoVO> list(UserInfoParam userInfoParam) {
        Page page = new Page(userInfoParam.getCurrent(), userInfoParam.getSize());
        return baseMapper.list(page,userInfoParam);
    }

    @Override
    public void add(UserInfoBO userInfoBO) {
        UserInfoDO userInfoDO  = new UserInfoDO();
        BeanUtil.copyPropertiesIgnoreNull(userInfoBO,userInfoBO);
        baseMapper.insert(userInfoDO);
        Long userId = userInfoDO.getId();

        //角色id
        List<Long> roleIdList = userInfoBO.getRoleIdList();
        if(CollectionUtils.isNotEmpty(roleIdList)){
            List<UserRoleRelationDO> userRoleList = roleIdList.stream().map(x->{
                return new UserRoleRelationDO().setUserId(userId).setIsDelete("0").setRoleId(x);
            }).collect(Collectors.toList());
            userRoleRelationService.saveBatch(userRoleList);
        }
        //岗位id
        List<Long> postIdList = userInfoBO.getPostIdList();
        if(CollectionUtils.isNotEmpty(postIdList)){
            List<UserPostRelationDO> userRoleList = postIdList.stream().map(x->{
                return new UserPostRelationDO().setUserId(userId).setIsDelete("0").setPostId(x);
            }).collect(Collectors.toList());
            userPostRelationService.saveBatch(userRoleList);
        }
    }

    @Override
    public void update(UserInfoBO userInfoBO) {
        UserInfoDO userInfoDO  = new UserInfoDO();
        BeanUtil.copyPropertiesIgnoreNull(userInfoBO,userInfoBO);
        baseMapper.updateById(userInfoDO);
    }

    @Override
    public void deleteById(Long id) {

        //删除用户的状态
        UserInfoDO userInfoDO  = new UserInfoDO();
        userInfoDO.setIsDelete("1");
        userInfoDO.setId(id);
        baseMapper.updateById(userInfoDO);

        //删除用户的角色
        userRoleRelationService.update(Wrappers.<UserRoleRelationDO>lambdaUpdate()
                .eq(UserRoleRelationDO::getUserId,id)
                .eq(UserRoleRelationDO::getIsDelete,"0")
                .set(UserRoleRelationDO::getIsDelete,"1")
        );

        //删除用户的岗位
        userPostRelationService.update(Wrappers.<UserPostRelationDO>lambdaUpdate()
                .eq(UserPostRelationDO::getIsDelete,"0")
                .eq(UserPostRelationDO::getUserId,id)
                .set(UserPostRelationDO::getIsDelete,"1")
        );
    }

    /**
     * 单表查询且数据量数据多的情况下可以使用该方案,将要查询的条件分离出来，使代码更加简洁
     * @param userInfoParam
     */
    public LambdaQueryWrapper<UserInfoDO> buildQueryWrapper(UserInfoParam userInfoParam){
        LambdaQueryWrapper<UserInfoDO> queryWrapper =Wrappers.lambdaQuery();
        //用户名称
        queryWrapper.like(StringUtils.isNotEmpty(userInfoParam.getUserName()), UserInfoDO::getUserName, userInfoParam.getUserName());
        //用户电话号码
        queryWrapper.like(StringUtils.isNotEmpty(userInfoParam.getPhoneNum()), UserInfoDO::getPhoneNum, userInfoParam.getPhoneNum());
        //用户账号
        queryWrapper.like(StringUtils.isNotEmpty(userInfoParam.getLoginAccount()), UserInfoDO::getLoginAccount, userInfoParam.getLoginAccount());
        //用户编码
        queryWrapper.like(StringUtils.isNotEmpty(userInfoParam.getUserCode()), UserInfoDO::getUserCode, userInfoParam.getUserCode());
        return queryWrapper;

    }
}
